<?php
include_once 'DBCon.php';
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Member
 *
 * @author Kenny
 */
class Member {
    private $icNo;
    private $firstname;
    private $lastname;
    private $uid;
    private $gender;
    private $address;
    private $city;
    private $state;
    private $postcode;
    private $contactno;
    private $dob;
    private $balance;
    private $point;
    private $con;
    private $registermember = 'INSERT INTO member(icNo, uid, firstname, lastname, gender, address, city, state, postalCode, contactNo, dob
                               ) VALUES(?,?,?,?,?,?,?,?,?,?,?);';
    private $selectuid = 'SELECT uid FROM users WHERE username = ?';
    private $selecticno = 'SELECT icNo FROM member WHERE icNo = ?;';
    private $geticno = 'SELECT icNo FROM member WHERE uid = ?;';
    private $selectdetails = 'SELECT firstname, lastname, gender, address, city, state, postalCode, contactNo, dob FROM member WHERE uid = ?;';
    private $updatedetails = 'UPDATE member SET firstname = ?, lastname = ?, gender = ?, address = ?, city = ?, state = ?, postalCode = ?, contactNo = ?, dob = ? WHERE uid = ?;';
    private $checkbalance = 'SELECT balance FROM member WHERE uid = ?;';
    private $updatebalance = 'update member set balance = ? where uid = ?;';
    private $checkpoint = 'SELECT point FROM member WHERE uid = ?;';
    private $updatepoint = 'update member set point = ? where uid = ?;';
    private $selectallmemberemail = 'select email from users;';
            
    function __construct() {
        $this->con = DBCon::connectDB();
    }
    
    public function createMember($username){
        $this->icNo = trim($_POST['icno']);
        $this->firstname = trim($_POST['firstname']);
        $this->lastname = trim($_POST['lastname']);
        $this->gender = trim($_POST['gender']);
        $this->address = trim($_POST['address']);
        $this->city = trim($_POST['city']);
        $this->state = trim($_POST['state']);
        $this->postcode = trim($_POST['postcode']);
        $this->contactno = trim($_POST['contactno']);
        $this->dob = trim($_POST['dob']);
        $pstmtselect = $this->con->prepare($this->selectuid);
        $pstmtinsert = $this->con->prepare($this->registermember);
        $pstmtselect->bind_param('s', $username);
        $pstmtselect->execute();
        $pstmtselect->store_result();
        $pstmtselect->bind_result($this->uid);
        if($pstmtselect->fetch()){
            $pstmtinsert->bind_param('sssssssssss', $this->icNo, $this->uid, $this->firstname,  $this->lastname,  $this->gender,  $this->address,  
                    $this->city, $this->state, $this->postcode, $this->contactno, $this->dob);
            $pstmtinsert->execute();
            if($pstmtinsert->affected_rows > 0){
                return true;
            }
        }
        return false;
    }
    
    public function checkIcno(){
        $icno = trim($_POST['icno']);
        $pstmtselect = $this->con->prepare($this->selecticno);
        $pstmtselect->bind_param('s', $icno);
        $pstmtselect->execute();
        if($pstmtselect->fetch()){
            $result = array(
                'error' => true,
                'message' => 'This IC number already registered as member'
            );
            return $result;
        }else{
            $result = array(
                'error' => false,
                'message' => ''
            );
            return $result;
        }
    }
    
    public function getDetails($uid){
        $pstmtselect = $this->con->prepare($this->selectdetails);
        $pstmtselect->bind_param('s', $uid);
        $pstmtselect->execute();
        $pstmtselect->bind_result($this->firstname,  $this->lastname,  $this->gender,  $this->address,  
                    $this->city, $this->state, $this->postcode, $this->contactno, $this->dob);
        if($pstmtselect->fetch()){
            $result = array(
                'firstname' => $this->firstname,
                'lastname' => $this->lastname,
                'gender' => $this->gender,
                'address' => $this->address,
                'city' => $this->city,
                'state' => $this->state,
                'postcode' => $this->postcode,
                'contactno' => $this->contactno,
                'dob' => $this->dob
            );
            return $result;
        }
    }
    
    public function updateDetails(){
        $firstname = trim($_POST['firstname']);
        $lastname = trim($_POST['lastname']);
        $gender = trim($_POST['gender']);
        $address = trim($_POST['address']);
        $city = trim($_POST['city']);
        $state = trim($_POST['state']);
        $postcode = trim($_POST['postcode']);
        $contactno = trim($_POST['contactno']);
        $dob = trim($_POST['dob']);
        $uid = trim($_POST['uid']);
        $pstmtupdate = $this->con->prepare($this->updatedetails);
        $pstmtupdate->bind_param('ssssssssss', $firstname, $lastname, $gender, 
                $address, $city, $state, $postcode, $contactno, $dob, $uid);
        $pstmtupdate->execute();
        if($pstmtupdate->affected_rows > 0){
            return true;
        }
        return false;
    }
    
    public function getMemberIC($uid){
        $pstmtselect = $this->con->prepare($this->geticno);
        $pstmtselect->bind_param('s', $uid);
        $pstmtselect->execute();
        $pstmtselect->bind_result($this->icNo);
        if($pstmtselect->fetch()){
            return $this->icNo;
        }
    }
    
    public function checkBalance($uid) {
        $pstmtselect = $this->con->prepare($this->checkbalance);
        $pstmtselect->bind_param('i', $uid);
        $pstmtselect->bind_result($this->balance);
        $pstmtselect->execute();
        if($pstmtselect->fetch()){
            return $this->balance;
        }
    }
    
    public function deduceBalance($balance, $uid){
        $pstmtinsert = $this->con->prepare($this->updatebalance);
        $pstmtinsert->bind_param('di', $balance, $uid);
        $pstmtinsert->execute();
        if($pstmtinsert->affected_rows > 0){
            return true;
        }
        return FALSE;
    }
    
    public function updateBalance($balance, $uid) {
        $pstmtinsert = $this->con->prepare($this->updatebalance);
        $old = $this->checkBalance($uid);
        $new = $old + $balance;
        $pstmtinsert->bind_param('di', $new, $uid);
        $pstmtinsert->execute();
        if($pstmtinsert->affected_rows > 0){
            return true;
        }
        return FALSE;
    }
    
    public function checkPoint($uid){
        $pstmtselect = $this->con->prepare($this->checkpoint);
        $pstmtselect->bind_param('i', $uid);
        $pstmtselect->bind_result($this->point);
        $pstmtselect->execute();
        if($pstmtselect->fetch()){
            return $this->point;
        }
    }
    
    public function addPoint($point, $uid) {
        $pstmtinsert = $this->con->prepare($this->updatepoint);
        $old = $this->checkPoint($uid);
        $new = $old + $point;
        $pstmtinsert->bind_param('ii', $new, $uid);
        $pstmtinsert->execute();
        if($pstmtinsert->affected_rows > 0){
            return true;
        }
        return FALSE;
    }
    
    public function deducePoint($point, $uid){
        $pstmtinsert = $this->con->prepare($this->updatepoint);
        $old = $this->checkPoint($uid);
        $new = $old - $point;
        $pstmtinsert->bind_param('ii', $new, $uid);
        $pstmtinsert->execute();
        if($pstmtinsert->affected_rows > 0){
            return $new;
        }
        return FALSE;
    }
    
    public function getAllMemberEmail(){
        $result = array();

        $pstmtSelect = $this->con->prepare($this->selectallmemberemail);
        $pstmtSelect->execute();
        $pstmtSelect->bind_result($email);
        
        while($pstmtSelect->fetch()){
            array_push($result, $email);
        }
        
        return $result;
    }
}



?>
